import Vue from "vue";
import { MenuData, MenuIcon } from "@/assets/js/menu";

export default {
  /**
   * 更新导航菜单 
   * @pkey  唯一标识
   * @title 名称
   * @pkey  后台定义的
   * @url   路由
   * @icon  图标
   * @return 新的导航菜单
   */
  updateMenuList: function({ menuList }) {
    let newMenuList = [],
      productPkey = "",
      mainUrl = "";
    menuList.forEach(item => {
      let subList = [];
      //二级菜单
      if (item.sub) {
        item.sub.forEach((subitem, subindex) => {
          let thirdList = [];
          
          //三级菜单
          if (subitem.sub) {
            subitem.sub.forEach((thirditem,thirdindex) => {
              if(thirdindex == 0 && MenuData[item.pkey] == "/business") {
                productPkey = thirditem.pkey;
              }
              let thirdMenu = {
                pkey: thirditem.pkey,
                title: thirditem.name,
                pkey: thirditem.pkey,
                url: MenuData[subitem.pkey] == "/business" ?  "/business/" + thirditem.pkey : MenuData[thirditem.pkey]                
              }
              if(subindex == 0 && thirdindex == 0) {
                mainUrl = MenuData[subitem.pkey] == "/business" ?  "/business/" + thirditem.pkey : MenuData[thirditem.pkey];
              }
              thirdList.push(thirdMenu);
            })
          }
          let subMenu = {
            pkey: subitem.pkey,
            title: subitem.name,
            pkey: subitem.pkey,
            sub: thirdList
          }
          subList.push(subMenu);
        });
      }
      //一级菜单 
      let List = {
        pkey: item.pkey,
        title: item.name,
        pkey: item.pkey,
        url: mainUrl ? mainUrl : MenuData[item.pkey],
        // url: MenuData[item.pkey] ?
        //           (MenuData[item.pkey] == "/business" ?
        //             "/business/" + productPkey : MenuData[item.pkey]) :
        //           undefined,
        sub: subList
      }
      newMenuList.push(List);
    })
    // console.log(newMenuList)
    return newMenuList;
  },
  /**
   * 检查手机号码是否符合要求
   * @param  {[type]} phone [需要检查的手机号码]
   * @return {[type]}       [true： 格式正确  false：格式错误]
   */
  checkMobile: (phone) => {
    return (/^1\d{10}$/.test(phone))
  },
  /**
   * 检查密码是否符合要求--只能输入6-20个字母、数字、下划线  
   * @param  {[type]} password [需要检查的密码]
   * @return {[type]}       [true： 格式正确  false：格式错误]
   */
  checkPassword: (password) => {
    return (/^(\w){6,20}$/.test(password))
  },
  /**
   * 校验email格式
   */
  checkEmail: (email) => {
    return(/[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/.test(email))
  },
}